<?php

namespace backend\controllers;

use common\models\gradeship\Gradeship;
use Yii;
use common\models\grade\Grade;
use common\models\grade\GradeSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use zhongkefu\zhongkefu;
use common\models\adminlog\AdminLog;
/**
 * GradeController implements the CRUD actions for Grade model.
 */
class GradeController extends BackendController
{


    /**
     * Lists all Grade models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new GradeSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Grade model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
		$model = $this->findModel($id);
		Adminlog::addlog(yii::$app->user->id, "查看会员等级 ".$model->name);
        return $this->render('view', [
            'model' => $model,
        ]);
    }

    /**
     * Creates a new Grade model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Grade();

        if ($model->load(Yii::$app->request->post()) ) {
            $model->gradeshipid = Yii::$app->request->post()['Grade']['gradeshipid'];
            $model->gradeshipvalue = '';

            if(!empty($model->gradeshipid)){
                $model->gradeshipvalue = Gradeship::findOne($model->gradeshipid)->value;
            }

            $model->save();
			$posiname = Yii::$app->request->post()['Grade']['name'];
			Adminlog::addlog(yii::$app->user->id, "添加会员等级 ".$posiname);
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing Grade model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post())) {
            $model->gradeshipid = Yii::$app->request->post()['Grade']['gradeshipid'];
            $model->gradeshipvalue = '';

            if(!empty($model->gradeshipid)){
                $model->gradeshipvalue = Gradeship::findOne($model->gradeshipid)->value;
            }
            $model->save();
			$posiname = Yii::$app->request->post()['Grade']['name'];
			Adminlog::addlog(yii::$app->user->id, "修改会员等级 ".$posiname);
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Grade model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
		$model->delete();
		Adminlog::addlog(yii::$app->user->id, '删除会员等级 '.$model->name);
        return $this->redirect(['index']);
    }
/**
     * 启用按钮
     * If update is successful, the browser will be redirected to the 'index' page.
     * @return mixed
     */
    public function actionStart()
    {
        if(isset(zhongkefu::$app->request->post()["checkboxid"])){
            $ids = zhongkefu::$app->request->post()["checkboxid"];
            $ids = explode(',',$ids);
            foreach($ids as $id){
                $model = Grade::findOne(['id' => $id]);
                Grade::updateAll(['status'=>Grade::STATUS_ACTIVE],['id' =>$model->id]);
            }
            return true;
        }
    }

    /**
     * 停用按钮
     * If update is successful, the browser will be redirected to the 'index' page.
     * @return mixed
     */
    public function actionStop()
    {

        if(isset(zhongkefu::$app->request->post()["checkboxid"])){
            $ids = zhongkefu::$app->request->post()["checkboxid"];
            $ids = explode(',',$ids);
            foreach($ids as $id){

                $model = Grade::findOne(['id' => $id]);
                Grade::updateAll(['status'=>Grade::STATUS_DELETED],['id' =>$model->id]);
            }
            return true;
        }
    }
    /**
     * 删除按钮
     * If update is successful, the browser will be redirected to the 'index' page.
     * @return mixed
     */
    public function actionAlldelete()
    {
		$artname = "";
        if(isset(zhongkefu::$app->request->post()["checkboxid"])){
            $ids = zhongkefu::$app->request->post()["checkboxid"];
            $ids = explode(',',$ids);
			foreach($ids as $val){
				$model = Grade::findOne(['id' => $val]);
				$artname .= $model->name.",";
			}
			$artname = substr($artname,0,-1);
            foreach($ids as $id){
                $model = Grade::findOne(['id' => $id]);
                $this->findModel($id)->delete();

            }
			Adminlog::addlog(yii::$app->user->id, '批量删除会员等级 '.$artname);
            return true;
        }
    }
    /**
     * Finds the Grade model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Grade the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Grade::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
